<template>
  <view class="uni-container">
    <uni-forms ref="form" :model="formData" validateTrigger="bind">
      <uni-forms-item name="order_id" label="订单编号" required>
        <uni-easyinput v-model="formData.order_id" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="order_child_id" label="子订单编号" required>
        <uni-easyinput v-model="formData.order_child_id" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="school_id" label="所属学校" required>
        <uni-data-picker v-model="formData.school_id" collection="admin-school-list" field="school_name as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="menu_id" label="所属菜单" required>
        <uni-data-picker v-model="formData.menu_id" collection="admin-home-menus" field="menu_name as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="yizhan_id" label="取件地址" required>
        <uni-data-picker v-model="formData.yizhan_id" collection="admin-yizhan-address" field="name as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="userAddress_id" label="用户地址" required>
        <uni-data-picker v-model="formData.userAddress_id" collection="admin-user-address" field="address as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="user_id" label="创建用户" required>
        <uni-easyinput v-model="formData.user_id"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="user_address_text" label="完整送货地址" required>
        <uni-easyinput type="textarea" placeholder="请输入送货地址的具体信息" v-model="formData.user_address_text" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="user_remark" label="订单备注" required>
        <uni-easyinput type="textarea" placeholder="请输入订单备注信息" v-model="formData.user_remark" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="expectedDelivery_id" label="预计送达日期" required>
        <uni-data-picker v-model="formData.expectedDelivery_id" collection="admin-send-time" field="name as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="expectedDelivery_min" label="最小时间" required>
        <uni-easyinput placeholder="请输入订单预计送达最小时间戳" v-model="formData.expectedDelivery_min" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="expectedDelivery_max" label="最大时间" required>
        <uni-easyinput placeholder="请输入订单预计送达最大时间戳" v-model="formData.expectedDelivery_max" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="expectedDelivery_text" label="预计送达时间" required>
        <uni-easyinput placeholder="请输入订单预计送达时间" v-model="formData.expectedDelivery_text" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="order_end_time" label="订单结束时间" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.order_end_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="order_out_time" label="订单超时时间" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.order_out_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="order_tips_money" label="订单小费" required>
        <uni-easyinput placeholder="请输入订单小费，直接奖励给骑手" v-model="formData.order_tips_money" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="order_all_money" label="订单总金额" required>
        <uni-easyinput placeholder="请输入订单订单总金额" v-model="formData.order_all_money" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="order_connect_phone" label="快递手机号" required>
        <uni-easyinput placeholder="请输入快递包裹上的手机号码" v-model="formData.order_connect_phone" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="pack_id" label="包裹类型" required>
        <uni-data-picker v-model="formData.pack_id" collection="admin-pack-categories" field="pack_type as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="verify_code" label="取件码" required>
        <uni-easyinput placeholder="请输入快递取件码" v-model="formData.verify_code" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="fast_status" label="是否加急" required>
        <switch style="transform: scale(0.8);" @change="binddata('fast_status', $event.detail.value)" :checked="formData.fast_status"></switch>
      </uni-forms-item>
      <uni-forms-item name="rider_order_get_status" label="是否接单" required>
        <switch style="transform: scale(0.8);" @change="binddata('rider_order_get_status', $event.detail.value)" :checked="formData.rider_order_get_status"></switch>
      </uni-forms-item>
      <uni-forms-item name="rider_order_get_time" label="接单时间" required>
        <uni-datetime-picker return-type="timestamp" v-model="formData.rider_order_get_time"></uni-datetime-picker>
      </uni-forms-item>
      <uni-forms-item name="rider_id" label="接单骑手" required>
        <uni-data-picker v-model="formData.rider_id" collection="uni-id-users" field="_id as text, _id as value"></uni-data-picker>
      </uni-forms-item>
      <uni-forms-item name="rider_order_status" label="订单状态" required>
        <uni-data-checkbox v-model="formData.rider_order_status" :localdata="formOptions.rider_order_status_localdata"></uni-data-checkbox>
      </uni-forms-item>
      <uni-forms-item name="rider_order_receipt" label="订单完成回执">
        <uni-file-picker ref="files" :auto-upload="false" file-mediatype="image" file-extname="jpg,png" :limit="3" return-type="array" v-model="formData.rider_order_receipt"></uni-file-picker>
      </uni-forms-item>
      <uni-forms-item name="three_pay_order_id" label="发送给第三方订单的编号" required>
        <uni-easyinput v-model="formData.three_pay_order_id" trim="both"></uni-easyinput>
      </uni-forms-item>
      <uni-forms-item name="three_pay_transaction_id" label="第三方订单支付状态回调的编号">
        <uni-easyinput v-model="formData.three_pay_transaction_id" trim="both"></uni-easyinput>
      </uni-forms-item>
      <view class="uni-button-group">
        <button type="primary" class="uni-button" style="width: 100px;" @click="submit">提交</button>
        <navigator open-type="navigateBack" style="margin-left: 15px;">
          <button class="uni-button" style="width: 100px;">返回</button>
        </navigator>
      </view>
    </uni-forms>
  </view>
</template>

<script>
  import { validator } from '../../js_sdk/validator/admin-order-list.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'admin-order-list';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "order_id": "",
        "order_child_id": "",
        "school_id": "",
        "menu_id": "",
        "yizhan_id": "",
        "userAddress_id": "",
        "user_id": "",
        "user_address_text": "",
        "user_remark": "",
        "expectedDelivery_id": "",
        "expectedDelivery_min": "",
        "expectedDelivery_max": "",
        "expectedDelivery_text": "",
        "order_end_time": null,
        "order_out_time": null,
        "order_tips_money": "",
        "order_all_money": "",
        "order_connect_phone": "",
        "pack_id": "",
        "verify_code": "",
        "fast_status": false,
        "rider_order_get_status": false,
        "rider_order_get_time": null,
        "rider_id": "",
        "rider_order_status": 0,
        "rider_order_receipt": [],
        "three_pay_order_id": "",
        "three_pay_transaction_id": ""
      }
      return {
        formData,
        formOptions: {
          "rider_order_status_localdata": [
            {
              "value": 0,
              "text": "待接单"
            },
            {
              "value": 1,
              "text": "已接单"
            },
            {
              "value": 2,
              "text": "已取货"
            },
            {
              "value": 3,
              "text": "派送中"
            },
            {
              "value": 4,
              "text": "已完成"
            },
            {
              "value": 5,
              "text": "已失败"
            },
            {
              "value": 6,
              "text": "已取消"
            },
            {
              "value": 7,
              "text": "已超时"
            },
            {
              "value": 8,
              "text": "待付款"
            },
            {
              "value": 9,
              "text": "退款/售后"
            },
            {
              "value": 10,
              "text": "支付超时，订单取消"
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    onLoad(e) {
      if (e.id) {
        const id = e.id
        this.formDataId = id
        this.getDetail(id)
      }
    },
    onReady() {
      this.$refs.form.setRules(this.rules)
    },
    methods: {
      
      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.getOpenerEventChannel().emit('refreshData')
          setTimeout(() => uni.navigateBack(), 500)
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("order_id,order_child_id,school_id,menu_id,yizhan_id,userAddress_id,user_id,user_address_text,user_remark,expectedDelivery_id,expectedDelivery_min,expectedDelivery_max,expectedDelivery_text,order_end_time,order_out_time,order_tips_money,order_all_money,order_connect_phone,pack_id,verify_code,fast_status,rider_order_get_status,rider_order_get_time,rider_id,rider_order_status,rider_order_receipt,three_pay_order_id,three_pay_transaction_id").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
